    <!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改实体用况')" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-interface-edit" th:object="${interfaceInfo}">
            <h4 class="form-header h4">接口信息</h4>
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">接口名称：</label>
                <div class="col-sm-8">
                    <input name="interfaceName" th:field="*{interfaceName}" class="form-control" type="text" disabled>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">接口描述：</label>
                <div class="col-sm-8">
                    <textarea name="interfaceDescription" class="form-control">[[*{interfaceDescription}]]</textarea>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">接口地址：</label>
                <div class="col-sm-8">
                    <input name="interfaceUrl" th:field="*{interfaceUrl}" class="form-control" type="text" disabled>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">前后类型：</label>
                <div class="col-sm-8">
                    <select name="fbType" class="form-control m-b" th:with="type=${@dict.getType('fbType')}" disabled>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{fbType}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">操作类型：</label>
                <div class="col-sm-8">
                    <select name="optType" class="form-control m-b" th:with="type=${@dict.getType('optType')}" disabled>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{optType}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">接口类型：</label>
                <div class="col-sm-8">
                    <select name="interfaceType" class="form-control m-b" th:with="type=${@dict.getType('interfaceType')}" disabled>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{interfaceType}"></option>
                    </select>
                </div>
            </div>
            <h4 class="form-header h4">子接口信息</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button type="button" class="btn btn-white btn-sm" onclick="addColumn()"><i class="fa fa-plus"> 增加</i></button>
                    <button type="button" class="btn btn-white btn-sm" onclick="delColumn()"><i class="fa fa-minus"> 删除</i></button>
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table" style="table-layout:fixed;"></table>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <script th:inline="javascript">
        var setVU = [[${@permission.hasPermi('interface:interface:setVU')}]];
        var setParam = [[${@permission.hasPermi('interface:interface:setParam')}]];
        if (setVU == 'hidden') {
            $("button").hide();
        }
        var interfaceDatas = [[${interfaceDatas}]];
        var prefix = ctx + "interface/interface";
        $("#form-interface-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/saveVirtualUsage", $('#form-interface-edit').serialize());
            }
        }
        var virtualUsages = [[${virtualUsages}]]

        $(function() {
            var options = {
                data: virtualUsages,
                pagination: false,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
                fixedColumns: true,
                fixedNumber: 4,
                columns: [{
                    checkbox: true
                },
                    {
                        field: 'index',
                        align: 'center',
                        title: "序号",
                        width: 50,
                        formatter: function (value, row, index) {
                            var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                            return columnIndex + $.table.serialNumber(index);
                        }
                    },
                    {
                        field: 'id',
                        title: '虚拟用况id',
                        width: 100,
                        formatter: function(value, row, index) {
                            var id = $.common.sprintf("<input type='hidden' name='virtualUsages[%s].id' value='%s'>", index, row.id);
                            return (value || "") + id;
                        }
                    },
                    {
                        field: 'interfaceId',
                        align: 'center',
                        title: '接口名称',
                        width: 200,
                        formatter: function(value, row, index) {
                            var name = $.common.sprintf("virtualUsages[%s].interfaceId", index);
                            if (row.id) {
                                var interfaceName = '';
                                if (interfaceDatas && interfaceDatas.length > 0) {
                                    for (var i=0; i<interfaceDatas.length; i++) {
                                        if (interfaceDatas[i].id == value) {
                                            interfaceName = interfaceDatas[i].interfaceName;
                                            break;
                                        }
                                    }
                                }
                                var interfaceName2 = $.common.sprintf("<input type='hidden' name='virtualUsages[%s].interfaceId' value='%s'>", index, value);
                                return interfaceName2 + interfaceName;
                            } else {
                                return dictToSelect(interfaceDatas, value, name);
                            }
                        }
                    },
                    {
                        field: 'description',
                        align: 'center',
                        title: '描述',
                        width: 300,
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input class='form-control' type='text' name='virtualUsages[%s].description' value='%s'>", index, value);
                            return html;
                        }
                    },
                    {
                        field: 'sortNum',
                        align: 'center',
                        title: '排序',
                        width: 50,
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input class='form-control' type='text' name='virtualUsages[%s].sortNum' value='%s'>", index, value);
                            return html;
                        }
                    }]
            };
            $.table.init(options);

            var select = $("table select");
            if (select && select.length > 0) {
                for (var i=0; i<select.length; i++) {
                    var value = [];
                    value.push(virtualUsages[i].interfaceId);
                    $(select[i]).select2();
                    $(select[i]).select2('val',value);
                }

            }
        });

        function dictToSelect(datas, value, name) {
            var actions = [];
            actions.push($.common.sprintf("<select class='form-control' name='%s'>", name));
            $.each(datas, function(index, dict) {
                actions.push($.common.sprintf("<option value='%s'", dict.id));
                if (dict.dictValue == ('' + value)) {
                    actions.push(' selected=true');
                }
                actions.push($.common.sprintf(">%s</option>", dict.interfaceName));
            });
            actions.push('</select>');
            return actions.join('');
        }

        function addColumn() {
            var select = $('table select')
            var selectArr = [];
            for (var i=0; i<select.length; i++) {
                selectArr.push($(select[i]).select2('val'))
            }

            var count = $("#" + table.options.id).bootstrapTable('getData').length;
            sub.editColumn();

            $("#" + table.options.id).bootstrapTable('insertRow', {
                index: count,
                row: {
                    index: $.table.serialNumber(count),
                    interfaceId: "",
                    description: "",
                    sortNum: ""
                }
            });
            var select2 = $('table select');
            for (var i=0; i<select2.length; i++) {
                $(select2[i]).select2({});
            }
            for (var i=0; i<selectArr.length; i++) {
                var data = [];
                data.push(selectArr[i]);
                $(select2[i]).select2('val', data);
            }
        }

        function delColumn(column) {
            var select = $('table select')
            var selectArr = [];
            for (var i=0; i<select.length; i++) {
                selectArr.push($(select[i]).select2('val'))
            }
            sub.editColumn();
            var subColumn = $.common.isEmpty(column) ? "index" : column;
            var indexs = $.table.selectColumns(subColumn);
            var ids = $.table.selectColumns("id");
            if (indexs.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            var arr = [];
            for (var i=0; i<ids.length; i++) {
                if (ids[i]) {
                    arr.push(ids[i]);
                }
            }
            if (arr.length > 0) {
                // 检查虚拟用况下的接口
                var config = {
                    url: prefix + '/checkVUId?vuIds='+arr.join(),
                    type: "post",
                    dataType: "json",
                    data: null,
                    success: function(result) {
                        if (result.code == web_status.SUCCESS) {
                            for (var i=0; i<indexs.length; i++) {
                                delete selectArr[indexs[i]-1];
                            }
                            $("#" + table.options.id).bootstrapTable('remove', { field: subColumn, values: indexs });

                            var select2 = $('table select');
                            for (var i=0; i<select2.length; i++) {
                                $(select2[i]).select2({});
                            }
                            var ii=0;
                            for (var i=0; i<selectArr.length; i++) {
                                if (selectArr[i]) {
                                    var data = [];
                                    data.push(selectArr[i]);
                                    $(select2[ii]).select2('val', data);
                                    ii++;
                                }
                            }
                        } else {
                            var select2 = $('table select');
                            for (var i=0; i<select2.length; i++) {
                                $(select2[i]).select2({});
                            }
                            var ii=0;
                            for (var i=0; i<selectArr.length; i++) {
                                if (selectArr[i]) {
                                    var data = [];
                                    data.push(selectArr[i]);
                                    $(select2[ii]).select2('val', data);
                                    ii++;
                                }
                            }
                            $.modal.alertWarning("虚拟用况接口有字段，请解除关联");
                        }
                    }
                };
                $.ajax(config)
            } else {
                for (var i=0; i<indexs.length; i++) {
                    delete selectArr[indexs[i]-1];
                }
                $("#" + table.options.id).bootstrapTable('remove', { field: subColumn, values: indexs });

                var select2 = $('table select');
                for (var i=0; i<select2.length; i++) {
                    $(select2[i]).select2({});
                }
                var ii=0;
                for (var i=0; i<selectArr.length; i++) {
                    if (selectArr[i]) {
                        var data = [];
                        data.push(selectArr[i]);
                        $(select2[ii]).select2('val', data);
                        ii++;
                    }
                }
            }
        }
    </script>
</body>
</html>